//
//  ZZQMainTopView.m
//  zzqKanBoKe
//
//  Created by ben on 17/2/7.
//  Copyright © 2017年 张增强. All rights reserved.
//

#import "ZZQMainTopView.h"

@interface ZZQMainTopView()

@property (nonatomic, strong)UIView * lineView;

@property (nonatomic, strong)NSMutableArray * btns;

@end

@implementation ZZQMainTopView

- (NSMutableArray *)btns{
    if(!_btns){
        _btns = [NSMutableArray array];
    }
    return _btns;
}

- (instancetype)initWithFrame:(CGRect)frame titleNames:(NSArray *)titles
{
    self = [super initWithFrame:frame];
    if (self) {
        CGFloat btnWidth = self.width/titles.count;
        CGFloat btnHeight = self.height;
        for(NSInteger i = 0; i < titles.count; i++){
            UIButton * titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            NSString * vcName = titles[i];
            //设置按钮文字
            [titleBtn setTitle:vcName forState:UIControlStateNormal];
            //设置颜色
            [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            //设置字体
            titleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
            titleBtn.frame = CGRectMake(btnWidth * i, 0, btnWidth, btnHeight);
            titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
            [titleBtn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
            titleBtn.tag = ZZQTopTypeFocue+i;
            [self addSubview:titleBtn];
            [self.btns addObject:titleBtn];
            
            //添加白线
            if(i == 1){
                CGFloat h = 2;
                CGFloat y = 38;
                [titleBtn.titleLabel sizeToFit];
                _lineView = [[UIView alloc] init];
                _lineView.backgroundColor = [UIColor whiteColor];
                _lineView.height = h;
                _lineView.width = titleBtn.titleLabel.width;
                _lineView.top = y;
                _lineView.centerX = titleBtn.centerX;
                [self addSubview:_lineView];
            }
        }
    }
    return self;
}
//点击事件
- (void)titleClick:(UIButton *)btn{
    self.block(btn.tag);
    [self titleButtonAnimate:btn];
}
//滚动联动
- (void)scrolling:(NSInteger)tag{
    
    UIButton * button = self.btns[tag];
    
    [self titleButtonAnimate:button];
}
//button下面的line动画
- (void)titleButtonAnimate:(UIButton *)btn{
    [UIView animateWithDuration:0.3 animations:^{
        self.lineView.centerX = btn.centerX;
    }];
}


@end
